//
//  JimDelightHashTable.cpp
//  CollectionTest
//
//  Created by Dusan Vujosevic on 2013-10-27.
//  Copyright (c) 2013 Suavetech. All rights reserved.
//

#include "JimDelightHashTable.h"
#include <math.h>

JimDelightHashTable::JimDelightHashTable(size_t elementCountHint):
	CollectionBase(elementCountHint), _tableSize(elementCountHint)
{
	_table = new STBucket<elemType>[_tableSize];
	size_t bitsNeeded = (size_t)log2f(elementCountHint);
	_mask = ~(~0 << bitsNeeded);
}

void JimDelightHashTable::reset()
{
	delete[] _table;
	_table = new STBucket<elemType>[_tableSize];
}

JimDelightHashTable::~JimDelightHashTable()
{
	delete[] _table;
}

void JimDelightHashTable::loadElements(const void* randBytes, size_t elemCount)
{
	elemType* randElem = (elemType*)randBytes;
	for(size_t u = 0; u < elemCount; u++)
	{
		elemType elem = *randElem++;
		size_t hashVal = hash(elem);
		_table[hashVal].AddElem(elem);
	}
}

bool JimDelightHashTable::findElement(elemType elem)
{
	size_t hashVal = hash(elem);
	return _table[hashVal].ContainsElem(elem);
}

template <typename Tkey>
void STBucket<Tkey>::AddElem(Tkey& elem)
{
	_elems.insert(elem);
}

template <typename Tkey>
bool STBucket<Tkey>::ContainsElem(const Tkey& elem) const
{
	std::set<elemType>::iterator res = _elems.find(elem);
	if(res != _elems.end())
		return true;
	else
		return false;
}
